package cn.yqx.bankshop.goods.dao.es;
import cn.yqx.bankshop.goods.config.ESClient;
import cn.yqx.bankshop.goods.esutil.ESResult;
import cn.yqx.bankshop.goods.pojo.BuGoods;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


@Slf4j
@Component
public class GoodsDslMapper<T> {
    @Autowired
    ESClient esClient ;
    private static final String INDEX_STR = "goods_laster" ;

    public ESResult queryGoods(Map<String,Object> param){
        ESResult result = esClient.sendQuery(INDEX_STR,"queryGoods",param) ;
        JSONArray productGradeThrees = new JSONArray() ;
        JSONObject resultAggs = result.getResultAggs() ;
        resultAggs.getJSONObject("productGradeThree").getJSONArray("buckets") ;
        JSONObject formatKeyName ;
        JSONArray formatKeyNames ;
        JSONObject productGradeFour ;
        JSONArray productGradeFours ;
        JSONObject productGradeThree;
        for(Object productGradeThreeFor: resultAggs.getJSONObject("productGradeThree").getJSONArray("buckets")){
            productGradeThree = new JSONObject() ;
            productGradeThree.put("doc_count",((JSONObject) productGradeThreeFor).getIntValue("doc_count")) ;
            productGradeThree.put("key",((JSONObject) productGradeThreeFor).getString("key")) ;

            productGradeFours = new JSONArray() ;
            for(Object productGradeFourFor :((JSONObject)productGradeThreeFor).getJSONObject("productGradeFour").getJSONArray("buckets")){
                productGradeFour = new JSONObject();
                formatKeyNames = new JSONArray() ;
                productGradeFour.put("doc_count",((JSONObject) productGradeFourFor).getIntValue("doc_count")) ;
                productGradeFour.put("key",((JSONObject) productGradeFourFor).getString("key")) ;
                for(Object formatKeyNameFor: ((JSONObject)productGradeFourFor).getJSONObject("goodsFormatKeys").getJSONObject("formatKeyName").getJSONArray("buckets")){
                    formatKeyName = new JSONObject( ) ;
                    formatKeyName.put("doc_count",((JSONObject) formatKeyNameFor).getIntValue("doc_count")) ;
                    formatKeyName.put("key",((JSONObject) formatKeyNameFor).getString("key")) ;
                    formatKeyName.put("formatValNames",((JSONObject) formatKeyNameFor).getJSONObject("goodsFormatVals").getJSONObject("formatValName").getJSONArray("buckets")) ;
                    formatKeyNames.add(formatKeyName) ;
                }
                productGradeFour.put("formatKeyNames",formatKeyNames) ;
                productGradeFours.add(productGradeFour) ;
            }
            productGradeThree.put("productGradeFours",productGradeFours) ;
            productGradeThrees.add(productGradeThree) ;
        }
        JSONObject productGradeThreeNew = new JSONObject() ;
        productGradeThreeNew.put("productGradeThrees",productGradeThrees) ;
        result.setResultAggs(productGradeThreeNew);
        return  result;
    }
}
